package common

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

// http请求
func RequestFabric(method, url string, data map[string]interface{}) (code int, bodyByte []byte, err error) {
	client := &http.Client{}

	bytesData, _ := json.Marshal(data)

	req, err := http.NewRequest(method, url, bytes.NewReader(bytesData))
	if err != nil {
		return 403, nil, fmt.Errorf("组装http req出错")
	}

	resp, err := client.Do(req)
	if err != nil {
		return 403, nil, fmt.Errorf("http请求出错")
	}
	bodyByte, _ = ioutil.ReadAll(resp.Body)

	return resp.StatusCode, bodyByte, nil
}
